package com.andy.blog.dal.service.impl;

import com.andy.blog.common.enums.DeleteTypeEnum;
import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.CommentDO;
import com.andy.blog.dal.domain.ReplyDO;
import com.andy.blog.dal.mapper.CommentMapper;
import com.andy.blog.dal.mapper.ReplyMapper;
import com.andy.blog.dal.service.CommentReplyDalService;
import com.andy.blog.dal.vo.CommentReplyVO;
import com.andy.blog.dal.vo.ReplyVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 评论 回复
 * Created by andy on 2023/7/19 17:43
 */
@Service
public class CommentReplyDalServiceImpl implements CommentReplyDalService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private ReplyMapper replyMapper;


    @Override
    public IPage<CommentDO> selectCommentByPage(CommentReplyVO commentReplyVO) {
        LambdaQueryWrapper<CommentDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Objects.nonNull(commentReplyVO.getArticleId()), CommentDO::getArticleId, commentReplyVO.getArticleId())
                .eq(StringUtils.isNotBlank(commentReplyVO.getCommentType()), CommentDO::getCommentType, commentReplyVO.getCommentType())
                .orderByDesc(CommentDO::getCreateTime);

        IPage<CommentDO> page = new Page<>(commentReplyVO.getPageNum(), commentReplyVO.getPageSize());

        return commentMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public List<CommentDO> selectList(CommentReplyVO commentReplyVO) {
        LambdaQueryWrapper<CommentDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Objects.nonNull(commentReplyVO.getArticleId()), CommentDO::getArticleId, commentReplyVO.getArticleId());

        return commentMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public void addComment(CommentReplyVO commentReplyVO) {
        CommentDO commentDO = TransformUtils.transform(commentReplyVO, CommentDO.class);

        commentDO.setIsDeleted(DeleteTypeEnum.DELETE_N.getCode());
        if (StringUtils.equals("FRIEND", commentDO.getCommentType())) {
            commentDO.setFromUserId(null);
        } else {
            commentDO.setFromUserId(SecurityUtils.getUserId());
        }

        try {
            commentMapper.insert(commentDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }
    }

    @Override
    public void addReply(ReplyVO replyVO) {
        ReplyDO replyDO = TransformUtils.transform(replyVO, ReplyDO.class);

        replyDO.setIsDeleted(DeleteTypeEnum.DELETE_N.getCode());
        replyDO.setFromUserId(SecurityUtils.getUserId());

        try {
            replyMapper.insert(replyDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }
    }

    @Override
    public List<ReplyDO> selectReplyList(CommentReplyVO commentReplyVO) {
        LambdaQueryWrapper<ReplyDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Objects.nonNull(commentReplyVO.getArticleId()), ReplyDO::getArticleId, commentReplyVO.getArticleId())
                .eq(Objects.nonNull(commentReplyVO.getCommentId()), ReplyDO::getCommentId, commentReplyVO.getCommentId())
                .eq(StringUtils.isNotBlank(commentReplyVO.getCommentType()), ReplyDO::getReplyType, commentReplyVO.getCommentType())
                ;

        return replyMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<CommentDO> listRecentComment() {
        LambdaQueryWrapper<CommentDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper
                .orderByDesc(CommentDO::getCreateTime)
                .last("limit 5");
        return commentMapper.selectList(lambdaQueryWrapper);
    }
}
